package sharewithus.solrclient.support;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Title:BeanDocConverter
 * Description: Bean与SolrInputDocument、SolrDocument转换类
 * @Create_by:Terry
 * @Create_date:2012-5-25
 * @Last_Edit_By:
 * @Edit_Description
 * @version:ShareWithUs 1.0
 *
 */
public class BeanDocConverter {
	
	private static Logger logger = LoggerFactory.getLogger(BeanDocConverter.class);
	
	/**
	 * 
	 * 2012-5-25, 上午8:36:52
	 * 方法描述：Bean转InputDoc
	 * @param bean
	 * @return SolrInputDocument
	 */
	@SuppressWarnings("unchecked")
	public static <T> SolrInputDocument bean2InputDoc(T bean){
		if(bean == null)
			throw new IllegalArgumentException();
		SolrInputDocument doc = new SolrInputDocument();
		try {
			Map<String,Object> properties = bean instanceof Map ?(Map<String,Object> ) bean: PropertyUtils.describe(bean);
			for(Map.Entry<String, Object> entry: properties.entrySet()){
				if(!"class".equalsIgnoreCase(entry.getKey()))
					doc.addField(entry.getKey(), entry.getValue());
			}
			return doc;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * 2012-5-25, 上午8:52:11
	 * 方法描述：doc转bean
	 * @param doc SolrDocument
	 * @param cls Class
	 * @return bean
	 */
	public static <T> T doc2bean(SolrDocument doc,Class<T> beanCls) {
		if(doc == null)
			throw new IllegalArgumentException();
		try {
			T bean = beanCls.newInstance();
			for(String field : doc.getFieldNames()){
				PropertyUtils.setProperty(bean, field, doc.getFieldValue(field));
			}
			return bean;
		}  catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * 2012-5-25, 上午9:04:16
	 * 方法描述：批量转换,bean集合转InputDoc集合
	 * @param beans
	 * @return
	 */
	public static <T> Collection<SolrInputDocument> beans2Inputdocs(Collection<T> beans) {
		if(beans == null)
			throw new IllegalArgumentException();
		Collection<SolrInputDocument> docs = new LinkedList<SolrInputDocument>();
		for(T bean : beans){
			SolrInputDocument inputdoc = bean2InputDoc(bean);
			docs.add(inputdoc);
		}
		return docs;
	}
	
	/**
	 * 
	 * 2012-5-25, 上午9:08:30
	 * 方法描述：批量转换,docList转bean集合
	 * @param docList
	 * @param cls
	 * @return
	 */
	public static <T> Collection<T> docs2beans(SolrDocumentList docList,Class<T> beanCls){
		if(docList == null)
			throw new IllegalArgumentException();
		List<T> beans = new LinkedList<T>();
		for (SolrDocument doc : docList) {
			T t = doc2bean(doc,beanCls);
			beans.add(t);
		}
		return beans;
	}
	
	public static SolrInputDocument doc2InputDoc(SolrDocument doc){
		if(doc == null)
			throw new IllegalArgumentException();
		SolrInputDocument inputDoc = new SolrInputDocument();
		Collection<String> fieldNames = doc.getFieldNames();
		for(String fieldName : fieldNames){
			inputDoc.addField(fieldName, doc.getFieldValue(fieldName));
		}
		return inputDoc;
	}
}
